﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Register;

namespace BranchOfSansar.Register.Authentication
{
    public partial class frmParameterList : Form
    {
        public frmParameterList()
        {
            InitializeComponent();
        }
        private string GroupCode
        {
            get {
                BranchOfSansar.Sansar.Enquiry.ComboItem item = (BranchOfSansar.Sansar.Enquiry.ComboItem)txtGroup.SelectedItem;
                return item.StrValue.Substring(0, 2);
            }
        }
        private void LoadData()
        {
            if (txtGroup.SelectedIndex >= 0)
                using (RegisterClient rq = new RegisterClient(Globals.Token))
                {                    
                    gridParameter.DataSource = rq.ParameterList(GroupCode);
                }
        }

        private void txtGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmParameterEdit frm=new frmParameterEdit(GroupCode);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                LoadData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            
            if (viewParameter.SelectedRowsCount < 1)
            {
                Globals.Warr("Өгөгдөл сонгогдоогүй байна!");
                return;
            }            
            Parameter para = (Parameter)viewParameter.GetRow(viewParameter.GetSelectedRows()[0]);
            frmParameterEdit frm = new frmParameterEdit(GroupCode, para);
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                LoadData();
            }
        }

        private void frmParameterList_Load(object sender, EventArgs e)
        {
            using (RegisterClient rq = new RegisterClient(Globals.Token))
            {
                List<BranchOfSansar.Sansar.Enquiry.ComboItem> list = rq.ParameterGroupList()
                    .Select(s => new BranchOfSansar.Sansar.Enquiry.ComboItem() { Display = s.Name, StrValue = s.ParaCode })
                    .ToList();

                txtGroup.Properties.Items.AddRange(list);
                txtGroup.SelectedIndex = 0;
            }
        }

    }
}
